<template>
  <div class="mainNav" :style="{zIndex: 100, position: 'relative'}">
    <van-tabbar v-model="tabIndex"
                active-color="#5E8EFE">
      <van-tabbar-item v-for="(item,index) in tabList"
                       :to="item.link"
        				:route="true"
                       :key="index"
						:dot="index == 2 && changeHot"
						@click="contactIm()">
        <img slot="icon"
             slot-scope="props"
             :src="props.active?item.activeicon:item.icon"
             alt="">
        <span>{{item.name}}</span>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
import {setRemind,getRemind,isContainKey,findIndex,unique,setAt,getAt,getVoice,fileterMsg} from '@/utils/chat.js'
import {getuserUid,getPassword} from '@/utils/auth.js'
import pushMsg from '@/utils/push.js'
import unread from '@/utils/unread.js'
export default {
  data() {
    return {
		hot:false,  //true则消息有红点提示
		userId:"", //用户id
		systemHot:0, //系统通知红点
		groups:[],
      tabList: [
        {
          name: "首页",
          icon: require("@/assets/img/tabBar/homePage.png"),
          activeicon: require("@/assets/img/tabBar/homePageHL.png"),
          link: "/performance/homePage"
        },
        {
          name: "订单",
          icon: require("@/assets/img/tabBar/orderCenter.png"),
          activeicon: require("@/assets/img/tabBar/orderCenterHL.png"),
          link: "/performance/orderCenter"
        },
        {
          name: "消息",
          icon: require("@/assets/img/tabBar/msgCenter.png"),
          activeicon: require("@/assets/img/tabBar/msgCenterHL.png"),
          link: "/performance/msgCenter"
        },
        {
          name: "我的",
          icon: require("@/assets/img/tabBar/personalCenter.png"),
          activeicon: require("@/assets/img/tabBar/personalCenterHL.png"),
          link: "/performance/personalCenter"
        }
      ]
    };
  },
	methods:{
		//登录环信
		contactIm:function(){
			console.log('登录环信');
			this.$imoption.user = getuserUid(); //环信账号
			this.$imoption.pwd = getPassword(); //环信密码
			this.$imconn.open(this.$imoption); //建立连接
		},
		//监听消息
		webIm:function(){
			// this.$imconn.listen({
			// 	//收到新消息
			// 	onTextMessage:data=>{
			// 		//自己发的消息不监听
			// 		if (data.from != this.userId) {
			// 			console.log('底部监听');
			// 			console.log(data);
			// 			//收到消息，红点提醒
			// 			if(data.type == "chatroom"){
			// 				this.$store.commit("serverHot", true);
			// 				pushMsg(data, "chatroom");
			// 			}
			// 			else{
			// 				this.$store.commit("setHot", true);
            //                 unread(data.to);
            //                 //写入缓存
            //                 let groups = getRemind()?getRemind():[];
            //                 groups.push(data.to);
            //                 let arr = unique(groups); //去重
            //                 setRemind(arr); //写入缓存
			// 				//推送
			// 				pushMsg(data, "group");
            //                 //判断是否有@
            //                 if(data.ext.ifAt){
            //                     //如果@的是我
            //                     let atArr = [];
            //                     if(getAt()){
            //                         atArr = getAt();
            //                     }
            //                     data.ext.toAt.map(o=>{
            //                         if(o == this.userId){
            //                             console.log('有人@我');
            //                             atArr.push(data.to);
            //                             let at = unique(atArr); //去重
            //                             setAt(at); //写入缓存
            //                         }
            //                     })
            //                 }
			// 			}
			// 		}
			// 	},
			// })
		},
		//系统通知红点提醒
		notice_hot:function(){
			HTTP('notice_hot',{
				userUid:getuserUid()
			})
			.then(res=>{
				if(res.code == 200){
					if(res.data){
						this.$store.commit("setSystemHot", true);
					}
					else{
						this.$store.commit("setSystemHot", false);
					}
					//this.systemHot = res.data;
				}
			})
		},
		//环信用户在线状态
		statusView:function(){
			HTTP('status',{
				userUid:getuserUid()
			})
			.then(res=>{
				if(res.code == 200){
					console.log(res.data);
				}
			})
		}
	},
	mounted:function(){
		this.notice_hot();
		console.log("hot:" + this.$store.state.hot);
		//监听非消息页消息
		if(this.$route.path != '/performance/msgCenter'){
			this.webIm();
		}
	},
	created:function(){
		this.userId = getuserUid();
		//有未读消息，开启红点
		let hots = getRemind();
		if(hots){
			if(hots.length>0){
				this.$store.commit("setHot", true);
				//this.hot = true;
			}
			else{
				this.$store.commit("setHot", false);
				//this.hot = false;
			}
		}
		else{
			this.$store.commit("setHot", false);
		}
	},
  computed: {
		//红点
		changeHot:function(){
			if(this.$store.state.systemHot || this.$store.state.hot || this.$store.state.hot2){
				return true;
			}
			else{
				return false;
			}
			//return this.$store.state.hot;
		},
    tabIndex: {
      get() {
        return this.$store.state.tabIndexP;
      },
      set(value) {
        //  console.log(this.$route.path)
        //  switch (this.$route.path) {

        //   case this.tabList[0].link:
        //     value = 0;
        //     break;
        //   case this.tabList[1].link:
        //     value = 1;
        //     break;
        //   case this.tabList[2].link:
        //     value = 2;
        //     break;
        //   case this.tabList[3].link:
        //     value = 3;
        //     break;
        // }
        this.$store.commit("setTabIndexP", value);
      },
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
